﻿/*
 * TextServices / TSDotNet / TSNative
 * Marco Maria Bellocchi, Mauro Bampo
 * Copyright(c) 2013.
 * 
 * The MIT License
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using TSDotNet.Common;

namespace TSDotNet.Encoders
{
    public class CombinatorialStringZipper : IEncoder
    {
        private IDictionary<string, char> _encoder = new Dictionary<string, char>();
        private IDictionary<char, string> _decoder = new Dictionary<char, string>();

        public CombinatorialStringZipper()
        {
            Initialize();
        }

        private void Initialize()
        {
            char[] chars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
            int charIndex = 0;
            for(int i = 0; i < chars.Length; i++)
            {
                _encoder.Add(chars[i].ToString(), chars[i]);
                _decoder.Add(chars[i], chars[i].ToString());
                for (int j = 0; j < chars.Length; j++)
                {
                    char doubleChar = CharsHelper.AllChars[charIndex++];
                    string tmp1 = String.Concat(chars[i], chars[j]);
                    _encoder.Add(tmp1, doubleChar);
                    _decoder.Add(doubleChar, tmp1);
                    for (int k = 0; k < chars.Length; k++)
                    {
                        char c=CharsHelper.AllChars[charIndex++];
                        string tmp = String.Concat(chars[i], chars[j], chars[k]);
                        _encoder.Add(tmp, c);
                        _decoder.Add(c, tmp);
                    }
                }
            }
        }

        public string Name
        {
            get { return "CombinatorialStringZipper"; }
        }

        public string Encode(string data)
        {
            StringBuilder sb = new StringBuilder();
            char[] chars = data.ToCharArray();
            int i = 0;
            StringBuilder tmp = new StringBuilder();
            foreach (char c in chars)
            {
                if (c == '-' || c == ',')
                {
                    if (i > 0)
                    {
                        sb.Append(_encoder[tmp.ToString()]);
                        tmp.Clear();
                    }
                    sb.Append(c);
                    i = 0;
                }
                else
                {
                    tmp.Append(c);
                    i++;
                }
                if (i == 3)
                {
                    sb.Append(_encoder[tmp.ToString()]);
                    tmp.Clear();
                    i = 0;
                }
            }
            if(i>0)
                sb.Append(_encoder[tmp.ToString()]);
            return sb.ToString();
        }

        public string Decode(string data)
        {
            StringBuilder sb = new StringBuilder();
            char[] chars = data.ToCharArray();
            foreach (char c in chars)
            {
                if (c == '-' || c == ',')
                {
                    sb.Append(c);
                }
                else
                {
                    sb.Append(_decoder[c]);
                }
            }
            return sb.ToString();
        }
    }
}
